//
//  ZZTabViewController.m
//  project
//
//  Created by 张峥 on 16/7/21.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "ZZTabViewController.h"
#import "HomePageViewController.h"
#import "ServiceViewController.h"
#import "ProductViewController.h"
#import "ListViewController.h"
#import "ProgressViewController.h"
#import "ZZConfig.h"
#import "RootViewController.h"
#import "ZZAnimation.h"

@interface ZZTabViewController ()
@property (nonatomic, strong) UIView             *tabbarView;
@property (nonatomic, strong) UIButton           *zzBtn;
@property (nonatomic, strong) UIButton           *lastBtn;
@property (nonatomic, strong) UIView             *goShopView;
@property (nonatomic, strong) UIView             *lastView;
@property (nonatomic, strong) RootViewController *rootVC;
@end

@implementation ZZTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = CGRectMake(0, 80,SCREEN_WIDTH, SCREEN_HEIGHT -80);
    //隐藏系统自带的tabbar
    self.tabBar.hidden = YES;
    [self creatNav];
    [self creatTabBar];
}

- (void)creatNav{
  
    
    
}


- (void)creatTabBar{
    //tabBar标签栏的高度为49
    _tabbarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0 , 80, 670)];
    _tabbarView.userInteractionEnabled = YES;
    _tabbarView.backgroundColor = barGrayColor;
    [self.view addSubview:_tabbarView];
    self.transitioningDelegate = self;
    self.modalPresentationStyle = UIModalPresentationCustom;
    
    HomePageViewController *homePageVC = [[HomePageViewController alloc] init];
//    homePageVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT -80);

    ServiceViewController *serviceVC = [[ServiceViewController alloc] init];
//    serviceVC.view.frame =  CGRectMake(0, (SCREEN_HEIGHT -80), SCREEN_WIDTH, SCREEN_HEIGHT -80);

    ProductViewController *prductVC = [[ProductViewController alloc] init];
//    prductVC.view.frame = CGRectMake(0, 2 * (SCREEN_HEIGHT -80), SCREEN_WIDTH, SCREEN_HEIGHT -80);
    ListViewController *listVC = [[ListViewController alloc] init];
//    listVC.view.frame = CGRectMake(0, 3 * (SCREEN_HEIGHT -80), SCREEN_WIDTH, SCREEN_HEIGHT -80);
    ProgressViewController *progressVC = [[ProgressViewController alloc] init];
//    progressVC.view.frame = CGRectMake(0, 4 * (SCREEN_HEIGHT -80), SCREEN_WIDTH, SCREEN_HEIGHT -80);
    self.viewControllers = @[homePageVC,serviceVC,prductVC,listVC,progressVC];
    NSArray *nomalImages = @[@"首页_03",@"出国服务_05",@"理财产品_07",@"选购清单_09",@"服务进度_11"];
    NSArray *selectImages = @[@"首页_03_h",@"出国服务_05_h",@"理财产品_07_h",@"选购清单_09_h",@"服务进度_11_h"];
    for (NSInteger i = 0; i < 5; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0 + 95 * i, 80, 95);
        btn.tag = 200 +i;
        [btn setImage:[UIImage imageNamed:nomalImages[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selectImages[i]] forState:UIControlStateSelected];
        btn.adjustsImageWhenHighlighted = NO;

        [btn addTarget:self action:@selector(btnTap:) forControlEvents:(UIControlEventTouchUpInside)];
        [_tabbarView addSubview:btn];
    }
    
    UIButton *trashBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    trashBtn.frame = CGRectMake(10, 610, 60, 50);
    [trashBtn addTarget: self action:@selector(trashBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
    [trashBtn setImage: [UIImage imageNamed:@"购物车_01"] forState:UIControlStateNormal];
    [_tabbarView addSubview:trashBtn];
}


#pragma mark   ---  代理方法
//返回转场动画过渡管理对象
- (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
                               interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController NS_AVAILABLE_IOS(7_0){
    return animationController;
}

#pragma mark   ---  点击事件

- (void)btnTap:(UIButton *)sender{
    UIButton *btn = [self.view viewWithTag:sender.tag];
    //先设置每一个的frame
    
    UIViewController *lastViewcontroller = [[UIViewController alloc] init];
    if (!_lastView) {
        lastViewcontroller = self.viewControllers[0];
        _lastView = lastViewcontroller.view;
        _lastView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }
    if (_lastBtn != nil && _lastBtn != btn) {
        _lastBtn.selected = NO;
        _lastBtn.userInteractionEnabled = YES;
    }
    if (btn != _lastBtn) {
        btn.selected = YES;
        btn.userInteractionEnabled = NO;

        self.selectedViewController = self.viewControllers[sender.tag - 200];
        self.selectedViewController.view.frame =  CGRectMake(0,-SCREEN_HEIGHT + 80, SCREEN_WIDTH, SCREEN_HEIGHT -80);
        [UIView animateWithDuration:10 animations:^{
            self.selectedViewController.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            _lastView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
        }];
         _lastBtn = btn;

    lastViewcontroller = self.selectedViewController;
   }
}
  


     
         
         


- (void)trashBtnTap:(UIButton *)sender{
    if (sender.selected == NO) {
        [self showView];
    }else{
        [self DisappearView];
    }
    sender.selected = !sender.selected;
}

#pragma mark   ---  自定义方法
/**弹出购物车*/
- (void)showView{
    _goShopView = [[UIView alloc] init];
    _goShopView.frame = CGRectMake(30, 30, 600, 400);
    _goShopView.backgroundColor = BLACK_C;
    _goShopView.alpha = 0.4;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:_goShopView];
}
/**收起购物车*/
- (void)DisappearView{
    _goShopView.alpha = 0;

}



@end
